/**
 * GeoDataGateway.java
 * 
 * Created on 20.05.2012 23:59:10
 */
package de.g18.scavengerhunt.service;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;

import de.g18.scavengerhunt.AppApplicationContext;
import de.g18.scavengerhunt.AppConstants;
import de.g18.scavengerhunt.activity.MapActivityInterface;
import de.g18.scavengerhunt.utils.NotificationUtil;

/**
 * This class listens for locations updates from the GPS sensor
 */
public class GeoDataGateway implements LocationListener {
    
	private final static String LOG_TAG = "GeoDataGateway";
    private LocationManager locationManager = null;
    private MapActivityInterface mapActivity = null;
    private static GeoPoint currentPositionGeoPoint = null;
    
    public GeoDataGateway(MapActivityInterface mapActivity) {
        this.mapActivity = mapActivity;
    }
    
    /**
     * Starts using the gps device.
     */
    public void start() {
    	Log.d(LOG_TAG, "Starting Location Service");
        locationManager = (LocationManager) AppApplicationContext.getCurrentActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
    
    /**
     * Stops using the gps device.
     */
    public void stop() {
    	Log.d(LOG_TAG, "Stopping Location Service");
    	if(locationManager != null) {
    		locationManager.removeUpdates(this); 
    	}
    }
    
    @Override
    public void onLocationChanged(Location location) {
    	if (location != null) {
			location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			try {
				currentPositionGeoPoint = createGeoPoint(location.getLatitude(), location.getLongitude());
				Log.d(this.getClass().getSimpleName(), "Raw position: lat=" + currentPositionGeoPoint.getLatitudeE6() + " lng=" + currentPositionGeoPoint.getLongitudeE6());
				Message msg = Message.obtain();
				msg.what = AppConstants.UPDATE_OWN_POSITION;
				msg.obj = currentPositionGeoPoint;
				mapActivity.onOverlayUpdate(msg);
			} catch (NullPointerException e) {
				Log.i(LOG_TAG, "Null pointer exception: " + location.getLongitude() + ", " + location.getLatitude());
			}
		} else {
			Toast.makeText(AppApplicationContext.getCurrentActivity().getApplicationContext(), "location = null", Toast.LENGTH_LONG).show();
		}
    }
    
    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(AppApplicationContext.getCurrentActivity().getApplicationContext(), "GPS ist deaktiviert.", Toast.LENGTH_SHORT).show();
        NotificationUtil.showNoGpsNotification();
        
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(AppApplicationContext.getCurrentActivity().getApplicationContext(), "GPS ist aktiviert", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    	//ignore
    }
    
    private GeoPoint createGeoPoint(double lat, double ltg) {
        return new GeoPoint((int)(lat * 1E6), (int)(ltg * 1E6));
    }
    
    public static GeoPoint getCurrentPositionGeoPoint() {
        return currentPositionGeoPoint;
    }
}